//
//  SEGFiveStarCell.m
//  YYKitDemo
//
//  Created by 刘山 on 2017/11/3.
//  Copyright © 2017年 Sam. All rights reserved.
//

#import "SEGFiveStarCell.h"
#import "WBTextLinePositionModifier.h"

#define kSEGFiveStarPadding 15
#define kSEGFiveStarContentWidth (LYScreenWidth() - 2 * kSEGFiveStarPadding)
#define kSEGFiveStarTextFontSize 15

@implementation SEGFiveStarCellModel
@end

@implementation SEGFiveStarCellLayout
- (instancetype)initWithData:(SEGFiveStarCellModel *)data{
    if (!data) return nil;
    if (self == [super init]){
        _data = data;
        _timeLayout = [SEGTimeLayout new];
        [self layout];
    }
    return self;
}

-(void)layout{
    _marginTop = 15;
    _contentHeight = 0;
    _linkHeight = 0;
    _marginBottom = 15;
    
    //  布局
    [self layoutContent];
    [self layoutLink];
    [self.timeLayout setLayoutWithData:_data.time];
    
    //  计算高度
    _height += _marginTop;
    _height += _contentHeight;
    _height += _linkHeight;
    _height += 10;
    _height += _marginBottom;
}

-(void)layoutContent{
    _contentHeight = 0;
    _contentLayout = nil;
    
    NSMutableAttributedString *text = [SEGKitHelper seg_TextWithString:_data.content fontSize:kSEGFiveStarTextFontSize textColor:kColor_1];
    
    if (text.length == 0) return;
    
    WBTextLinePositionModifier *modifier = [WBTextLinePositionModifier new];
    modifier.font = [UIFont fontWithName:@"Heiti SC" size:15];
    modifier.paddingTop = 0;
    modifier.paddingBottom = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(kSEGFiveStarContentWidth, HUGE);
    
    _contentLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_contentLayout) return;
    
    _contentHeight = [modifier heightForLineCount:_contentLayout.rowCount];
}

-(void)layoutLink{
    _linkHeight = 0;
    _linkLayout = nil;
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithString:@"点击查看奖状"];
    [text yy_setColor:kColor_S3 range:text.yy_rangeOfAll];
    [text yy_setFont:kFont_15 range:text.yy_rangeOfAll];
    
    // 高亮状态的背景
    SEGTextBorder *highlightBorder = [SEGTextBorder new];
    highlightBorder.insets = UIEdgeInsetsMake(-2, 0, -2, 0);
    highlightBorder.cornerRadius = 3;
    highlightBorder.fillColor = kColor_S3;
    
    // 高亮状态
    SEGTextHighlight *highlight = [SEGTextHighlight new];
    [highlight setBackgroundBorder:highlightBorder];
    // 数据信息，用于稍后用户点击
    highlight.userInfo = @{@"TOPIC" : text};
    [text yy_setTextHighlight:highlight range:text.yy_rangeOfAll];
    
    WBTextLinePositionModifier *modifier = [WBTextLinePositionModifier new];
    modifier.font = [UIFont fontWithName:@"Heiti SC" size:kSEGFiveStarTextFontSize];
    modifier.paddingTop = 0;
    modifier.paddingBottom = 0;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(kSEGFiveStarContentWidth, HUGE);
    
    _linkLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_linkLayout) return;
    
    _linkHeight = [modifier heightForLineCount:_linkLayout.rowCount];
}
@end

@implementation SEGFiveStarCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    __weak typeof(self) _self = self;
    
    _contentLabel = [SEGLabel new];
    _contentLabel.left = kSEGFiveStarPadding;
    _contentLabel.width = kSEGFiveStarContentWidth;
    _contentLabel.textVerticalAlignment = SEGTextVerticalAlignmentTop;
    _contentLabel.displaysAsynchronously = YES;
    _contentLabel.ignoreCommonProperties = YES;
    _contentLabel.fadeOnAsynchronouslyDisplay = NO;
    _contentLabel.fadeOnHighlight = NO;
    _contentLabel.hidden = YES;
    _contentLabel.highlightTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        if ([_self.delegate respondsToSelector:@selector(seg_openLinkUrl:)]) {
            [_self.delegate seg_openLinkUrl:_self.layout.data];
        }
    };
    
    _linkLabel = [SEGLabel new];
    _linkLabel.left = kSEGFiveStarPadding;
    _linkLabel.width = LYScreenWidth() * 0.5;
    _linkLabel.textVerticalAlignment = SEGTextVerticalAlignmentTop;
    _linkLabel.displaysAsynchronously = YES;
    _linkLabel.ignoreCommonProperties = YES;
    _linkLabel.fadeOnAsynchronouslyDisplay = NO;
    _linkLabel.fadeOnHighlight = NO;
    _linkLabel.hidden = YES;
    _linkLabel.highlightTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        if ([_self.delegate respondsToSelector:@selector(seg_openLinkUrl:)]) {
            [_self.delegate seg_openLinkUrl:_self.layout.data];
        }
    };
    
    _timeView = [SEGTimeView new];
    
    [self.contentView addSubview:_contentLabel];
    [self.contentView addSubview:_linkLabel];
    [self.contentView addSubview:_timeView];
    return self;
}

- (void)setLayout:(SEGFiveStarCellLayout *)layout{
    _layout = layout;
    self.height = layout.height;
    self.contentView.height = layout.height;
    
    CGFloat top = layout.marginTop;
    
    if (layout.contentHeight >0) {
        _contentLabel.top = top;
        _contentLabel.textLayout = layout.contentLayout;
        _contentLabel.height = layout.contentHeight;
        _contentLabel.hidden = NO;
        
        top += layout.contentHeight;
    }else{
        _contentLabel.hidden = YES;
    }
    
    if (layout.linkHeight >0) {
        _linkLabel.top = top + 10;
        _linkLabel.textLayout = layout.linkLayout;
        _linkLabel.height = layout.linkHeight;
        _linkLabel.hidden = NO;
    }else{
        _linkLabel.hidden = YES;
    }
    
    if (layout.timeLayout.timeSize.height >0) {
        _timeView.hidden = NO;
        [_timeView setLayout:layout.timeLayout];
        _timeView.bottom = self.height - kSEGFiveStarPadding;
        _timeView.right = LYScreenWidth() - kSEGFiveStarPadding;
    }else{
        _timeView.hidden = NO;
    }
}
@end
